package com.littleken.filter;

import com.littleken.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter("*.do")
public class PowerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResp = (HttpServletResponse) servletResponse;
        User user = (User) httpReq.getSession().getAttribute("user");
        if(user!=null){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            httpResp.sendRedirect(httpReq.getContextPath()+"/loginPage.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
